﻿using System;

#if TRIAL_BUILD
using Microsoft.Win32;
#endif

namespace LeanEngine.Extensions
{
    /// <summary>
    /// Extension methods for the data type System.DateTime.
    /// </summary>
    public static class DateTimeExtensions
    {
        #region Parse Extensions Members

        /// <summary>
        /// Converts the current value for an database valid datetime or 01/01/1900 when is an invalid value.
        /// </summary>
        public static DateTime ToDatabaseDateTime(this DateTime date)
        {
            DateTime defaultDbDate = new DateTime(1900, 1, 1);
            return (date == DateTime.MinValue ||
                    date == DateTime.MaxValue ||
                    date < defaultDbDate)
                    ? defaultDbDate
                    : date;
        }

        #endregion

        #region Validation Extensions Members

        /// <summary>
        /// Evaluates if the current value is a valid date and greater than or equal 01/01/1900.
        /// </summary>
        public static bool IsValidDate(this DateTime date)
        {
            return date >= new DateTime(1900, 1, 1);
        }

        /// <summary>
        /// Evaluates if the current value has a default date.
        /// </summary>
        /// <remarks>
        /// The default value is considered System.DateTime.MinValue, System.DateTime.MaxValue
        /// or is less than or equals 01/01/1900.
        /// </remarks>
        public static bool HasDefaultDate(this DateTime date)
        {
            return (date == DateTime.MinValue ||
                date == DateTime.MaxValue ||
                date <= new DateTime(1900, 1, 1));
        }

        /// <summary>
        /// Evaluates if the current value has not a default date.
        /// </summary>
        /// <remarks>
        /// The default value is considered System.DateTime.MinValue, System.DateTime.MaxValue
        /// or is less than or equals 01/01/1900.
        /// </remarks>
        public static bool HasNotDefaultDate(this DateTime date)
        {
            return !date.HasDefaultDate();
        }

        /// <summary>
        /// Evaluates whether the current value is between two comparison values.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="begin">The lower comparison value.</param>
        /// <param name="end">The higher comparison value.</param>
        public static bool IsBetween(this DateTime date, DateTime begin, DateTime end)
        {
            return date >= begin && date <= end;
        }

        /// <summary>
        /// Evaluates whether the current value is greater than the comparison value.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="compareDate">The comparison value.</param>
        public static bool IsGreaterThan(this DateTime date, DateTime compareDate)
        {
            return date > compareDate;
        }

        /// <summary>
        /// Evaluates whether the current value is greater than or equal the comparison value.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="compareDate">The comparison value.</param>
        public static bool IsGreaterThanEqual(this DateTime date, DateTime compareDate)
        {
            return date >= compareDate;
        }

        /// <summary>
        /// Evaluates whether the current value is less than the comparison value.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="compareDate">The comparison value.</param>
        public static bool IsLessThanEqual(this DateTime date, DateTime compareDate)
        {
            return date <= compareDate;
        }

        /// <summary>
        /// Evaluates whether the current value is less than or equal the comparison value.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="compareDate">The comparison value.</param>
        public static bool IsLessThan(this DateTime date, DateTime compareDate)
        {
            return date < compareDate;
        }

        /// <summary>
        /// Evaluates whether the current value not is between two comparison values.
        /// </summary>
        /// <param name="date">The current value.</param>
        /// <param name="begin">The lower comparison value.</param>
        /// <param name="end">The higher comparison value.</param>
        public static bool NotIsBetween(this DateTime date, DateTime begin, DateTime end)
        {
            return date < begin && date > end;
        }

        #endregion

        #region Method Extensions Members

        /// <summary>
        /// Remove the change in date / time caused by summer time.
        /// </summary>
        public static void ResolveSumTime(this DateTime date)
        {
            date = DateTime.SpecifyKind(date, DateTimeKind.Unspecified);
        }

        #endregion
    }
}
